package com.insist.controller;

import com.insist.framework.CmmBaseResponse;
import com.insist.model.CartProductModel;
import com.insist.service.CartService;
import com.insist.util.CmmUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;

import java.util.List;

/**
 * Created by Administrator on 2016/6/30.
 */
@Controller
@RequestMapping(value = "/cart")
public class CartController {

    @Autowired
    CartService cartService;

    @RequestMapping(value = "/addCart")
    @ResponseBody
    public CmmBaseResponse addCart(Long id,Integer number){
        cartService.addCart(id, CmmUtils.getLoginUser().getId(),number);

        return CmmBaseResponse.buildResponse(true, "添加成功");
    }

    /**
     *
     * @param id productId
     * @return
     */
    @RequestMapping(value = "/editCart")
    @ResponseBody
    public CmmBaseResponse editCart(Long id,Integer num){
        cartService.editCart(id, num, CmmUtils.getLoginUser().getId());
        return CmmBaseResponse.buildResponse(true, "添加成功");
    }
    /**
     *
     * @param id productId
     * @return
     */
    @RequestMapping(value = "/delCart")
    @ResponseBody
    public CmmBaseResponse delCart(Long id){
        cartService.delCart(id, CmmUtils.getLoginUser().getId());
        return CmmBaseResponse.buildResponse(true, "添加成功");
    }

    @RequestMapping(value = "/getCart")
    @ResponseBody
    public List<CartProductModel> getCart(){
        return cartService.getCart(CmmUtils.getLoginUser().getId());
    }

    @RequestMapping(value = "/getCartByProductId")
    @ResponseBody
    public CartProductModel getCart(Long productId){
        CartProductModel model =  cartService.getCart(CmmUtils.getLoginUser().getId(),productId);
        model.setPicture(CmmUtils.replacePictureUrl(model.getPicture()));
        return model;
    }
}
